<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Task as TaskModel;

/**
 * 首页接口
 */
class Task extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $model = new TaskModel();
        $list = $model->with(['user.companyName'])
            ->where('type_id', TaskModel::TYPE_X)
            ->where('status', TaskModel::STATUS_N)
            ->where('audit_status', TaskModel::AUDIT_STATUS_PASS)
            ->order(['is_top' => 'desc', 'id' => 'desc'])->limit(3)->select();
        $this->success('', $list);
    }

    public function list()
    {
        $title = $this->request->get('title', '');
        $type_id = \app\common\model\Task::TYPE_X;
        $user_id = $this->request->get('user_id', '');

        $model = new TaskModel();
        $list = $model->with(['user.companyName'])
            ->where('audit_status', TaskModel::AUDIT_STATUS_PASS)
            ->where(function ($query) use ($title, $type_id, $user_id) {
                if ($title) {
                    $query->where('title', 'like', '%' . $title . '%');
                }
                if ($type_id) {
                    $query->where('type_id', $type_id);
                }
                if ($user_id) {
                    $query->where('user_id', $user_id);
                }
            })->order(['status' => 'asc', 'is_top' => 'desc', 'id' => 'desc'])->select();
        $this->success('', $list);
    }

    public function info()
    {
        $id = $this->request->get('id');
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $data = \app\common\model\Task::get($id, ['userAll']);
        $data->user = $data->userAll->hidden(['password', 'salt', 'money']);
        unset($data->user_all, $data->userAll);
        $this->success('', $data);
    }

    public function save()
    {
        $userId = $this->auth->id;
        $type_id = \app\common\model\Task::TYPE_X;
        $title = $this->request->post('title');
        $content = $this->request->post('content');

        if (!$type_id || !$title || !$content) {
            $this->error(__('Invalid parameters'));
        }

        TaskModel::create([
            'user_id' => $userId,
            'type_id' => $type_id,
            'title' => $title,
            'content' => $content,
        ]);
        $this->success();
    }

    public function update()
    {
        $userId = $this->auth->id;
        $id = $this->request->post('id');
        $type_id = \app\common\model\Task::TYPE_X;
        $title = $this->request->post('title');
        $content = $this->request->post('content');
        $status = $this->request->post('status');

        if (((!$type_id || !$title || !$content) && is_null($status)) || !$id) {
            $this->error(__('Invalid parameters'));
        }

        $task = TaskModel::get($id);
        if (!$task) {
            $this->error(__('Invalid parameters'));
        }
        if ($task->user_id != $userId) {
            $this->error(__('Invalid parameters'));
        }
        $task->save(is_null($status) ? [
            'type_id' => $type_id,
            'title' => $title,
            'content' => $content,
            'audit_status' => $task->audit_status == TaskModel::AUDIT_STATUS_REFUSE ? TaskModel::AUDIT_STATUS_WAIT : $task->audit_status,
        ] : [
            'status' => $status,
        ]);
        $this->success();
    }

    public function del()
    {
        $userId = $this->auth->id;
        $id = $this->request->post('id');
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }

        $task = TaskModel::get($id);
        if (!$task) {
            $this->error(__('Invalid parameters'));
        }
        if ($task->user_id != $userId) {
            $this->error(__('Invalid parameters'));
        }
        $task->delete();
        $this->success();
    }
}
